package com.jeesite.modules.dp.web.stocktmp;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.dp.entity.stocktmp.DpTmpjson;
import com.jeesite.modules.dp.service.stocktmp.DpTmpjsonService;
import com.jeesite.modules.sys.utils.UserUtils;

/**
 * 选股模版管理Controller
 * 
 * @author hancing.com
 * @version 2024-12-26
 */
@Controller
@RequestMapping(value = "${adminPath}/dp/stocktmp/dpTmpjson")
public class DpTmpjsonController extends BaseController {

	@Autowired
	private DpTmpjsonService dpTmpjsonService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DpTmpjson get(String id, boolean isNewRecord) {
		return dpTmpjsonService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("dp:stocktmp:dpTmpjson:view")
	@RequestMapping(value = { "list", "" })
	public String list(DpTmpjson dpTmpjson, Model model) {
		model.addAttribute("dpTmpjson", dpTmpjson);
		return "modules/dp/stocktmp/dpTmpjsonList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("dp:stocktmp:dpTmpjson:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DpTmpjson> listData(DpTmpjson dpTmpjson, HttpServletRequest request, HttpServletResponse response) {
		dpTmpjson.setPage(new Page<>(request, response));
		return dpTmpjsonService.findPage(dpTmpjson);
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("dp:stocktmp:dpTmpjson:view")
	@RequestMapping(value = "form")
	public String form(DpTmpjson dpTmpjson, Model model) {
		model.addAttribute("dpTmpjson", dpTmpjson);
		return "modules/dp/stocktmp/dpTmpjsonForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("dp:stocktmp:dpTmpjson:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DpTmpjson dpTmpjson) {
		dpTmpjsonService.save(dpTmpjson);
		return renderResult(Global.TRUE, text("保存选股模版成功！"));
	}

	@PostMapping(value = "frontlist")
	@ResponseBody
	public List<DpTmpjson> frontlist(DpTmpjson dpTmpjson) {
		// 查询系统内置模板
		dpTmpjson.setIszb("0");
		dpTmpjson.setIssys("1");
		List<DpTmpjson> sysMbList = dpTmpjsonService.findList(dpTmpjson);

		// 查询自己创建的模板
		dpTmpjson=new DpTmpjson();
		dpTmpjson.setUserno(UserUtils.getUser().getLoginCode());
		List<DpTmpjson> myMbList = dpTmpjsonService.findList(dpTmpjson);

		// 去重
		for(DpTmpjson mb : myMbList) {
			if(sysMbList.stream().noneMatch(item->item.getId().equals(mb.getId()))) {
				sysMbList.add(mb);
			}
		}

		return sysMbList;
	}

	@PostMapping(value = "frontdelusermb")
	@ResponseBody
	public String delusermb(DpTmpjson dpTmpjson) {
		dpTmpjsonService.delete(dpTmpjson);
		return renderResult(Global.TRUE, text("删除模版成功！"));
	}

	/**
	 * 保存数据(前端不验证)
	 */

	@PostMapping(value = "frontsave")
	@ResponseBody
	public String frontsave(DpTmpjson dpTmpjson) {
		dpTmpjson.setIsNewRecord(dpTmpjson.getId()==null);
		if(dpTmpjson.getId()==null) {
			dpTmpjson.setIssys("0");
			dpTmpjson.setIszb("0");
		}
		dpTmpjson.setUserno(UserUtils.getUser().getLoginCode());
		dpTmpjsonService.save(dpTmpjson);
		return renderResult(Global.TRUE, text("保存选股模版成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("dp:stocktmp:dpTmpjson:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DpTmpjson dpTmpjson) {
		dpTmpjsonService.delete(dpTmpjson);
		return renderResult(Global.TRUE, text("删除选股模版成功！"));
	}

}